package com.dorm.cj.thread.threadGroup;

import java.util.Random;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {

        ThreadGroup threadGroup = new ThreadGroup("hello");
        for (int i = 0; i < 10; i++) {
            new Thread(threadGroup, () -> {
                try {
                    TimeUnit.SECONDS.sleep(new Random().nextInt(5));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        while (threadGroup.activeCount() >= 1) {
            System.out.println("--------------------------------------------------");
            int count = threadGroup.activeCount();
            System.out.println("count:" + count);
            Thread[] threads = new Thread[count];
            int enumerate = threadGroup.enumerate(threads);
            System.out.println(enumerate);
            System.out.println(threads.length);
            for (int i = 0; i < count; i++) {
                System.out.println(threads[i].getName() + " state: " + threads[i].getState());
            }
        }
        System.out.println("====================================================================================");
        int count2 = threadGroup.activeCount();
        System.out.println("count2 :" + count2);
        Thread[] threads = new Thread[count2];
        int enumerate = threadGroup.enumerate(threads);
        System.out.println(enumerate);
        System.out.println(threads.length);
        for (int i = 0; i < count2; i++) {
            System.out.println(threads[i].getName() + " state: " + threads[i].getState());
        }
    }
}
